// cd /projects/hsieh_project/proj_201809/code_2_201910/
// qstata out_cind_con_d_city_emp.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_201910/out_cind_con_d_city_emp_log, replace text

clear all
capture noi program drop _all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/201910_main/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/201910_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/cind_con_d_city_emp/"
*capture noi rmdir ${dir_outf}
capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

// Load rounding functions
do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_lpolyci.do"

tempfile ds_temp
global ds_temp = "`ds_temp'"

/*
*Clear excel files in folder
local excel_files: dir "${dir_outf}" files "*.xlsx"
foreach file of local excel_files {
	rm "${dir_outf}`file'"
}
*/
//==============================================================================
// Changes in concentration at city-industry level

/*
--------------------------------------------------------------------------------
Function that reads in the city-ind data and calculates difference
i_city = city code
v_sector = variable name of sector variable
c_mfg = 1 = using MfG only
*/

capture program drop read_city_d
program read_city_d
	args i_city v_sector c_mfg
	
	di "`i_city'"
	//local i_city = "msa"
	
	use "${dir_data}/cityind_sum_all_sel", clear
	keep if city_code == "`i_city'"
	
	if "`c_mfg'" == "1" {
		// Use only MfG
		keep if sector == 1
	}
	
	keep if year1 == ${year1} & year2 == ${year2}

	sort city ch_ind year
	
	// After collapse, c_* becomes city level variables aggregated from city-ind level
	gen c_est_cind = est_cind
	gen c_emps_ind_10 = emp_ind_10 / emp_cind
	gen c_emps_cind_t1 = emp_cind_t1 / emp_cind
	
	// Calculate ln difference
	foreach i_var of var c_emps_ind_10 c_emps_cind_t1 c_est_cind {
		gen ln_`i_var' = ln(`i_var')
		by city ch_ind: gen ln_`i_var'_d = ln_`i_var'[2] - ln_`i_var'[1]
	}
	
	// Difference related to HHI
	gen c_hhi = hhi_cind
	gen c_hhin = hhin_cind
	gen c_hhir = hhir_cind
	foreach i_var of var c_hhi c_hhin c_hhir {
		by city ch_ind: gen `i_var'_d = `i_var'[2] - `i_var'[1]
		by city ch_ind: gen ln_`i_var'_d = ln(`i_var'[2]) - ln(`i_var'[1])
	}
	
	// Each city-ind should have two identical rows 
	// (we exclude city-in in only one year, as sv weight also excludes city-ind in only one years)
	keep if year == 1977
	
	// Aggregate differences to the city level with sv weight
	collapse (first) emp_city_${year1} (mean) ln_c_emps_* ln_c_est_cind_d *c_hhi_* *c_hhin_* *c_hhir_* [w=w_sva], by(city `v_sector') // Use new weight 200129

	foreach i_var of var emp_city_$year1 {
		gen ln_`i_var' = ln(`i_var')
	}
	
end

/*
--------------------------------------------------------------------------------
Function that plots
Y = change in city-ind concentration aggregated to city level
X = initial city size
*/

capture program drop fig_city_d_emp
program fig_city_d_emp
	args i_city i_var t_ytitle t_title t_subtitle f_weight i_sect
	/*
	twoway (scatter `i_var' ln_emp_city_${year1}, msize(small)), ///
		ytitle("`t_ytitle'") xtitle("ln(City Employment in ${year1})") ///
		title("`t_title'") subtitle("`t_subtitle'")
	graph export "${dir_outf}/scat_`i_var'_c_emp_${year1}_`i_city'`f_weight'.png", replace width(3000) height(2000)
	*/
	
	lpolyci_data "" "lpoly_fig" "`i_var'" "ln_emp_city_${year1}" "99" "100"
	frame lpoly_fig {
		preserve
		keep `i_var'_x `i_var'_s `i_var'_se
		rounding_4dig "`i_var'_x `i_var'_s `i_var'_se"
		
		export excel ${dir_outf}/lpoly_cind_con_d_city_emp.xlsx, sheet("`i_city'_`i_var'") sheetreplace firstrow(var) keepcellfmt
		shell chmod a+rw "${dir_outf}/lpoly_cind_con_d_city_emp.xlsx" 
		restore
		
		local v_y1 = "`i_var'"
		
		if "`i_city'"=="fips" {
			foreach i_suf in cil ciu s x {
				replace `v_y1'_`i_suf' = . if `v_y1'_x <5
				//replace `v_y2'_`i_suf' = . if `v_y2'_x <5
			}
			local c_xlab "xlab(5(2)15)"
		}
		else if inlist("`i_city'","msa","msa0") {
			foreach i_suf in cil ciu s x {
				replace `v_y1'_`i_suf' = . if `v_y1'_x <9
				//replace `v_y2'_`i_suf' = . if `v_y2'_x <9
			}
			local c_xlab "xlab(9(2)15)"
		}
		
		if "`i_city'" == "msa" {
			local t_city = "MSA"
			local t_cityc = "M0F*"
			local t_cityc2 = "M0F*"
		}
		if "`i_city'" == "fips" {
			local t_city = "FIPS"
			local t_cityc = "M*F0"
			local t_cityc2 = "M0F0"
		}
		if "`i_var'" == "ln_c_emps_cind_t1_d" local t_sample = "ALL, CINDT1*_`t_city'_CINDNEW_`t_cityc', CINDNEW_M*F*"
		if "`i_var'" == "c_hhi_d" local t_sample = "ALL, CINDNEW_`t_cityc2', CINDNEW_M*F*"
		if "`i_var'" == "ln_c_est_cind_d" local t_sample = "ALL, CINDNEW_`t_cityc2', CINDNEW_M*F*"
		
		twoway (rarea `v_y1'_cil `v_y1'_ciu `v_y1'_x, color(none) fi(inten0) lc(gs8) lw(thin)) ///
			(connected `v_y1'_s `v_y1'_x, m(i) lcolor(navy) lw(medthick)), ///
			legend(off) ///
			ytitle("`t_ytitle'") xtitle("ln(City Employment in ${year1})") ///
			title("`t_title'") subtitle("`t_subtitle', 99% CI") `c_xlab' note("Sample: `t_sample'")
		graph export "${dir_outf}/lpoly_`i_var'_c_emp_${year1}_`i_city'`f_weight'.png", replace width(3000) height(2500)
	}
end

//------------------------------------------------------------------------------
// Program that produces figure for the given geo

capture program drop fig_group
program fig_group
	args i_city t_weight f_weight i_sect
	
	local t_city = upper("`i_city'")
	
	//fig_city_d_emp "`i_city'" "ln_c_emps_ind_10_d" "d ln(Share of Top 10% Ind Firm)" "d ln(Share of Top 10% Ind Firm)" "`t_city'`t_weight'" "`f_weight'" "`i_sect'"
	fig_city_d_emp "`i_city'" "ln_c_emps_cind_t1_d" "d ln(Share of Top 1 City-Ind Firm)" "d ln(Share of Top 1 City-Ind Firm)" "`t_city'`t_weight'" "`f_weight'" "`i_sect'"
	fig_city_d_emp "`i_city'" "c_hhi_d" "d HHI" "d HHI" "`t_city'`t_weight'" "`f_weight'" "`i_sect'"
	fig_city_d_emp "`i_city'" "ln_c_hhi_d" "d ln(HHI)" "d ln(HHI)" "`t_city'`t_weight'" "`f_weight'" "`i_sect'"
	fig_city_d_emp "`i_city'" "ln_c_est_cind_d" "d ln(Est)" "d ln(Est)" "`t_city'`t_weight'" "`f_weight'" "`i_sect'"
end


//==============================================================================
// Aggreate across all industries (Basic geo)

global dir_outf "${dir_out}/cind_con_d_city_emp"
capture mkdir ${dir_outf}

local i_city = "fips"
local t_city = upper("`i_city'")
read_city_d "`i_city'"
fig_group "fips" ", SV Weighted" "_sv" "0"

// MSA1983
local i_city = "msa1983"
local t_city = upper("`i_city'")
read_city_d "`i_city'"
fig_group "msa1983" ", SV Weighted" "_sv" "0"

//==============================================================================
// Aggreate across all industries (Improved geo)

global dir_outf "${dir_out}/cind_con_d_city_emp_msa_czone/"
capture mkdir ${dir_outf}


//==============================================================================
// Aggreate across MfG

if 0 {
global dir_outf "${dir_out}/cind_con_d_city_emp_mfg"
capture mkdir ${dir_outf}

local i_city = "fips"
local t_city = upper("`i_city'")
read_city_d "`i_city'" "" "1"
fig_group "fips" ", MfG, SV Weighted" "_sv_mfg"
}


//==============================================================================
// Aggreate by sectors

if 0 {
global dir_outf "${dir_out}/cind_con_d_city_emp_sect"
capture mkdir ${dir_outf}


// FIPS
global i_city = "fips"
global t_city = upper("${i_city}")
// SV weight
read_city_d "${i_city}" "sector"
levelsof sector, local(l_sect)
foreach i_sect in `l_sect' {
	preserve
	keep if sector == `i_sect'
	fig_group "fips" ", Sector `i_sect', SV Weighted" "_sect`i_sect'_sv"
	restore
}
}

di "Ended at $S_DATE $S_TIME"
capture noi log close
// End of do file
